package com.cloud.mall.cms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.cms.dao.CmsSubjectDao;
import com.cloud.mall.cms.mapper.CmsSubjectMapper;
import com.cloud.mall.cms.model.CmsSubject;
import com.cloud.mall.cms.model.CmsSubjectExample;
import com.cloud.mall.cms.out.CmsSubjectOut;
import com.cloud.mall.cms.service.CmsSubjectService;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CmsSubjectServiceImpl implements CmsSubjectService {
    @Autowired
    private CmsSubjectMapper subjectMapper;
    @Autowired
    private CmsSubjectDao cmsSubjectDao;


    @Override
    public BaseResponse<List<CmsSubjectOut>> list() {
        List<CmsSubject> subjectList=subjectMapper.selectByExample(new CmsSubjectExample());
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(subjectList,CmsSubjectOut.class));
    }

    @Override
    public BaseResponse<CommonPage<CmsSubjectOut>> getList(String keyword, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        CmsSubjectExample example = new CmsSubjectExample();
        CmsSubjectExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andTitleLike("%" + keyword + "%");
        }
        List<CmsSubject> subjectList = subjectMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(subjectList,CmsSubjectOut.class)));
    }

    @Override
    public BaseResponse<List<CmsSubjectOut>> getRecommendSubjectList(Integer pageNum, Integer pageSize) {
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(cmsSubjectDao.getRecommendSubjectList(pageNum,pageSize),CmsSubjectOut.class));
    }

    @Override
    public BaseResponse<CommonPage<CmsSubjectOut>> getSubjectList(Long cateId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        CmsSubjectExample example = new CmsSubjectExample();
        CmsSubjectExample.Criteria criteria = example.createCriteria();
        criteria.andShowStatusEqualTo(1);
        if(cateId!=null){
            criteria.andCategoryIdEqualTo(cateId);
        }
        List<CmsSubject> cmsSubjects = subjectMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(cmsSubjects,CmsSubjectOut.class)));
    }
}
